/**
 * @author yuyangyang
 * @time 2015/3/11
 */
var _ = require('underscore');
var logger = require('../utils/logger').getLogger('default');
var sportService = require('../services/sport');
var meetService = require('../services/meet');
var userService = require('../services/user');

module.exports = {
    mapping: '/sport',
    get: {
        '/': function (req, res) {
            sportService.queryAll(function (err, sportList) {
                meetService.queryAll(function (err, meetList) {
                    var countObj = {};
                    _.each(meetList, function (item) {
                        if (!countObj[item.sport]) {
                            countObj[item.sport] = 0;
                        }
                        countObj[item.sport]++;
                    });

                    _.each(sportList, function (item) {
                        item._doc.count = countObj[item.id] === undefined ? 0 : countObj[item.id];
                    });
                    res.json(sportList);
                });

            });
        },
        '/:id': function (req, res) {
            sportService.queryById(req.params.id, function (err, sport) {
                res.json(sport);
            });
        }
    },
    post: {
        '/new': function (req, res) {
            sportService.addSport({name: req.body.name}, function (err, data) {
                res.json(data);
            });
        }
    }
}